<?php
/**
 * Created by PhpStorm.
 * User: Xzareth
 * Date: 20/05/14
 * Time: 9:33 PM
 */
require 'init.php';

if (isset($_POST['submit'])) {

    $userID = $_REQUEST['userID'];
    $billID = $_REQUEST['billID'];
    $shipID = $_REQUEST['shipID'];
    $orderID = $_REQUEST['orderID'];
    $status = $_REQUEST['status'];
    $total = $_REQUEST['total'];
    $shipDate = $_REQUEST['shipDate'];

    $insertOrderFormStatus = array();
    $modifyFormStatus = array();

    if ($_REQUEST['submit'] == "X") {
        $modifyFormStatus[] = $order->deleteOrder($orderID);
    }
    if ($_REQUEST['submit'] == "Insert") {
        $insertOrderFormStatus[] = ($order->createOrder($userID, $billID, $shipID, $status, $total, $shipDate)) ? 'Successfully created order' : 'Unable to create order';
    } else if ($_REQUEST['submit'] == "Update") {
        $modifyFormStatus[] = $order->updateOrder($orderID, $status, $total, $shipDate);
    }
}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link href="css/main.css" rel="stylesheet" type="text/css">
    <title>Orders</title>
</head>
<body>
<div id="container">
    <div id="leftCogSplash"><img src="images/cogLeft.png"/></div>
    <div id="rightCogSplash"><img src="images/cogRight.png"/></div>
    <?php include "navbar.php"; ?>
    <div id="content">
        <div id="adminDashboard">
            <div id="returnToDashboard">
                <a href="admin.php"><h4>Return to Dashboard</h4></a>
            </div>
            <?php include "orderDashboard.php"; ?>
            <div id="currentOrders">
                <h3>All Current Orders</h3>
            </div>
            <div id="orderResults">
                <?php
                $rowNumber = 0;
                $sql = "SELECT * FROM Orders";

                if (isset($_GET['orderResults'])) {
                    echo "<script type='text/javascript'>
                            document.getElementById('orderResults').style.display='block';
                            document.getElementById('modifyFormContainer').style.display='none';
                            </script>";
                }

                if (isset($_GET['status'])) {
                    $sql = "SELECT * FROM Orders WHERE OrderStatus = '$_GET[status]'";
                } else if (isset($_GET['orderID'])) {
                    $sql = "SELECT * FROM Orders WHERE OrderID = '$_GET[orderID]'";
                } else {
                    $sql = "SELECT * FROM Orders";
                }

                foreach ($db->query($sql) as $row) {
                    $rowNumber++;
                    ?>
                    <table>
                        <thead <?php if ($rowNumber !== 1) echo "style='display: none'" ?>>
                        <tr>
                            <th class="orderIDCol">Order ID</th>
                            <th class="userIDCol">User ID</th>
                            <th class="billingIDCol">Billing ID</th>
                            <th class="shippingIDCol">Shipping ID</th>
                            <th class="orderDateCol">Order Date</th>
                            <th class="orderStatusCol left">Order Status</th>
                            <th class="totalPriceCol">Total Price</th>
                            <th class="shippingDateCol">Shipping Date</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td class="orderIDCol"><?php echo $row['OrderID']; ?></td>
                            <td class="userIDCol"><?php echo $row['UserID']; ?></td>
                            <td class="billingIDCol"><?php echo $row['BillingID']; ?></td>
                            <td class="shippingIDCol"><?php echo $row['ShippingID']; ?></td>
                            <td class="orderDateCol"><?php echo $row['OrderDate']; ?></td>
                            <td class="orderStatusCol left"><?php echo $row['OrderStatus']; ?></td>
                            <td class="totalPriceCol right"><?php echo number_format(($row['TotalPrice']), 2, ".", ""); ?></td>
                            <td class="shippingDateCol"><?php echo $row['ShippingDate']; ?></td>
                        </tr>
                        </tbody>
                    </table>
                <?php
                }
                ?>
            </div>
            <div id="modifyFormContainer">
                <?php
                if (isset($_GET['modifyOrders'])) {
                    echo "<script type='text/javascript'>
                            document.getElementById('orderResults').style.display='none';
                            document.getElementById('modifyFormContainer').style.display='block';
                            </script>";
                }
                $rowNumber = 0;
                $sql = "SELECT * FROM Orders";
                foreach ($db->query($sql) as $row) {
                    $rowNumber++;
                    ?>
                    <form id="modifyForm" name="modifyForm" method="post" action="">
                        <table>
                            <thead <?php if ($rowNumber !== 1) echo "style='display: none'" ?>>
                            <tr>
                                <th class="orderIDCol"><label>ID</label></th>
                                <th class="userIDCol"><label>User ID</label></th>
                                <th class="billingIDCol"><label>Bill ID</label></th>
                                <th class="shippingIDCol"><label>Ship ID</label></th>
                                <th class="orderDateCol"><label>Order Date</label></th>
                                <th class="orderStatusCol center"><label for='status'>Status</label></th>
                                <th class="totalPriceCol right"><label for='total'>Total</label></th>
                                <th class="shippingDateCol"><label for='shipDate'>Ship Date</label></th>
                                <th></th>
                                <th></th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr>
                                <td class="orderIDCol"><?php echo $row['OrderID']; ?></td>
                                <td class="userIDCol"><?php echo $row['UserID']; ?></td>
                                <td class="billingIDCol"><?php echo $row['BillingID']; ?></td>
                                <td class="shippingIDCol"><?php echo $row['ShippingID']; ?></td>
                                <td class="orderDateCol"><?php echo $row['OrderDate']; ?></td>
                                <td class="orderStatusCol left"><input type='text' name='status' id='status'
                                                                       value='<?php echo $row['OrderStatus']; ?>'/></td>
                                <td class="totalPriceCol right"><input type='text' name='total' id='total'
                                                                       value='<?php echo number_format(($row['TotalPrice']), 2, ".", ""); ?>'/>
                                </td>
                                <td class="shippingDateCol"><input type='text' name='shipDate' id='shipDate'
                                                                   value='<?php echo $row['ShippingDate']; ?>'/></td>
                                <td class="updateBtn"><input type="submit" name="submit" value="Update"/></td>
                                <td class="deleteBtn"><input type='submit' name='submit' value='X' class='red'
                                                             id="deleteButton"></td>
                            </tr>
                            </tbody>
                        </table>
                        <input type='hidden' name='orderID' id='orderID' value='<?php echo $row['OrderID']; ?>'/>
                    </form>
                <?php
                }
                ?>
            </div>
            <div id="modifyFormStatusBar">
                <?php
                if (!empty($modifyFormStatus)) {
                    echo "<script type='text/javascript'>document.getElementById('modifyFormStatusBar').style.display = 'block';</script>";
                    echo '<p>' . implode('<p></p>', $modifyFormStatus) . '</p>';
                    echo "<script type='text/javascript'>clearMessage('modifyFormStatusBar', 2000)</script>";
                }
                // close the database connection
                $db = null;
                ?>
            </div>
        </div>
        <div id="push"></div>
        <?php require 'footer.php' ?>
    </div>
</div>
</body>
</html>